package cn.bouncyslime.graduationProject.nba.crawler.pipeline;

import cn.bouncyslime.graduationProject.nba.dao.TeamDao;
import cn.bouncyslime.graduationProject.nba.crawler.spider.NewTeamPageProcessor;
import cn.bouncyslime.graduationProject.nba.dto.TeamDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * 作者： 张一卓
 * 时间： 2020/3/27 23:07
 */
@Component
public class TeamPipeline implements Pipeline {

    private static final Logger logger = LoggerFactory.getLogger(NewTeamPageProcessor.class);

    @Autowired
    TeamDao teamDao;

    @Override
    public void process(ResultItems resultItems, Task task) {
//        logger.info("headers: " + task.getSite().getHeaders());
        TeamDto teamDto = resultItems.get("team");
        if (teamDto != null) {
            try {
                teamDao.insertTeam(teamDto);
            }catch (Exception e) {
                logger.error("插入球队数据失败，team：" + teamDto);
            }
        }
    }
}
